package amazing.core.module;

import amazing.core.main.AmazingServer.ModuleManager;

public abstract class Module implements AutoCloseable {
	
	public abstract void open() throws ModuleFailedToLoadException;
	
	/***
	 * Required constructor for any instantiated modules
	 * gives the ModuleManager instance information regarding who loaded this class
	 * @param loader the class loading this module, only either by ModuleManager or from a RequiredModule directive where the module was not already loaded
	 */
	public Module()
	{
		StackTraceElement[] stes = new Throwable().getStackTrace();
		if (stes.length < 3 || !stes[stes.length - 3].getClassName().equals(ModuleManager.class.getName()))
			throw new IllegalStateException("You are not allowed to instantiate modules from any class but amazing.core.module.ModuleManager. ["+stes[stes.length-3]+"]");
	}
	/***
	 * Called from ModuleManager when a module has been added (this module is excluded). Module.open() will already have been called
	 * @param m the module added
	 */
	public void notifyModuleAdded(Module m){}
	
	/***
	 * Called from ModuleManager when a module has been removed (this module is excluded).  Module.close() will already have been called
	 * @param m the module removed
	 */
	public void notifyModuleRemoved(Module m){}
}
